package com.weblog.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weblog.weblog.common.domain.dos.CommentDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author gc
 * @date 2024年03月06日 18:21
 */

public interface CommentMapper extends BaseMapper<CommentDO> {

    /**
     * 根据路由地址查询评论
     * @author gc
     * @date 2024/3/7 22:35
     * @param routerUrl
     * @param status
     * @return java.util.List<com.weblog.weblog.common.domain.dos.CommentDO>
     */
    default List<CommentDO> selectByRouterUrlAndStatus(String routerUrl,Integer status){
        return selectList(Wrappers.<CommentDO> lambdaQuery()
                .eq(CommentDO::getRouterUrl,routerUrl)
                .eq(CommentDO::getStatus,status)
                .orderByDesc(CommentDO::getCreateTime)
        );
    }


    /**
     * 分页查询
     * @author gc
     * @date 2024/3/16 22:34
     * @param current
     * @param size
     * @param routerUrl
     * @param startDate
     * @param endDate
     * @param status
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.weblog.weblog.common.domain.dos.CommentDO>
     */
    default Page<CommentDO> selectPageList(Long current, Long size, String routerUrl, LocalDate startDate, LocalDate endDate,Integer status){

        Page<CommentDO> page = new Page<>(current,size);

        //构建查询条件
        LambdaQueryWrapper<CommentDO> wrapper = Wrappers.<CommentDO>lambdaQuery()
                .like(StringUtils.isNotBlank(routerUrl),CommentDO::getRouterUrl,routerUrl)
                .eq(Objects.nonNull(status),CommentDO::getStatus,status)
                .ge(Objects.nonNull(startDate), CommentDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), CommentDO::getCreateTime, endDate)
                .orderByDesc(CommentDO::getCreateTime);

        return selectPage(page,wrapper);
    }


    /**
     * 根据 reply_id 查询评论
     * @author gc
     * @date 2024/3/16 23:06
     * @param replyCommentId
     * @return java.util.List<com.weblog.weblog.common.domain.dos.CommentDO>
     */
    default List<CommentDO> selectByReplyCommentId(Long replyCommentId){
        return selectList(Wrappers.<CommentDO>lambdaQuery()
                .eq(CommentDO::getReplyCommentId,replyCommentId)
                .orderByDesc(CommentDO::getCreateTime)
        );
    }

    /**
     * 根据 parent_comment_id 删除
     * @param id
     * @return
     */
    default int deleteByParentCommentId(Long id) {
        return delete(Wrappers.<CommentDO>lambdaQuery()
                .eq(CommentDO::getParentCommentId, id));
    }

}
